package com.qf.springcloud.filters;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyPreFilter01 extends ZuulFilter {

    //返回的就是当期那过滤器的类型
    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    //返回过滤器的顺序, 在相同的 type 的情况下,数字越小,优先级越高
    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext=RequestContext.getCurrentContext();
        HttpServletRequest request= requestContext.getRequest();//获取请求
        HttpServletResponse response= requestContext.getResponse();
        String requestURI = request.getRequestURI();
        System.err.println(requestURI);
        //前置通知可以中断请求的运行
        //requestContext.setSendZuulResponse(false);//拦截请求
        //拦截之后设置返回结果
        response.setContentType("text/html;charset=utf-8");
        requestContext.put("suibian", "2020-04-03老罗喝酒后说想要个妞");
        System.err.println("MyPreFilter01执行了");
        return null;
    }
}
